﻿using BlogSystem.Data.Entity;
using BlogSystem.Model.Request;
using BlogSystem.Service;
using Microsoft.AspNetCore.Authorization;
using Microsoft.AspNetCore.Mvc;

namespace BlogSystem.Controller;


/// <summary>
///  评论 控制器
/// </summary>
[Authorize]
[ApiController]
[Route("api/[controller]")]
public class RemarkController : ControllerBase
{
    private readonly IRemarkService _remarkService;

    public RemarkController(IRemarkService iRemarkService)
    {
        _remarkService = iRemarkService;
    }
    
    
    /// <summary>
    ///  获取评论列表
    /// </summary>
    /// <param name="keyWord">关键词</param>
    /// <param name="pageIndex">页码</param>
    /// <param name="pageSize">页面大小</param>
    /// <returns></returns>
    [HttpGet]
    public async Task<IActionResult> GetRemarks([FromQuery] string? keyWord="",[FromQuery] int pageIndex=1,[FromQuery] int pageSize=10)
    {
        
        var remarkvo = await _remarkService.GetRemarksAsync(keyWord,pageIndex,pageSize);
        return Ok(new {code="200",msg="成功",total=remarkvo.Count(),article=remarkvo});

    }
    
    // 根据该条评论的id进行查找
    [HttpGet("{id}")]
    public async Task<IActionResult> GetById(int id)
    {
        Remark remark =await _remarkService.GetById(id);
        if (remark == null)
            return NoContent();
        else
            return Ok(remark);
    }
    
    [HttpPost]
    public async Task<IActionResult> AddRemark(RemarkRequest remarkRequest)
    {
        
        await _remarkService.AddRemark(remarkRequest);
        return Ok(new { msg = "添加成功" });
    }

    [HttpPut]
    public async Task<IActionResult> UpdateRemark(int id,RemarkRequest remarkRequest)
    {
        await _remarkService.UpdateRemark(id,remarkRequest);
        return Ok(new { msg = "修改成功" });
    }
    
    
    //删  - 逻辑删除
    [HttpDelete("{id}")]
    public async Task<IActionResult> DelRemarkById(int id)
    {
        bool res = await _remarkService.DelRemarkById(id);
        if(res)
            return Ok("删除成功。");
        else
            return BadRequest("无此数据。");
    }
    
    //针对 文章的id 找出对应的 评论内容
    [HttpGet("{articleId}/remarks")]
    public async Task<IActionResult> GetByArticleId(int articleId)
    {
        List<Remark> remark = await _remarkService.GetByArticleId(articleId);
        if (remark == null)
            return NoContent();
        else
            return Ok(remark);
    }

    
}